﻿//-----------------------------------------------------------------------
// <copyright file="AboutPage.cs" company="In The Hand Ltd">
//     Copyright © 2014-15 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using InTheHand.ApplicationModel;
#if V81
using Windows.ApplicationModel.DataTransfer;
#else
using InTheHand.ApplicationModel.DataTransfer;
#endif

namespace InTheHand.UI.ApplicationSettings.About
{
    public partial class AboutPage : PhoneApplicationPage
    {
        public AboutPage()
        {
            InitializeComponent();

            /*if (System.Globalization.CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft())
            {
                this.FlowDirection = System.Windows.FlowDirection.RightToLeft;
            }*/

            
            Microsoft.Phone.Marketplace.LicenseInformation license = new Microsoft.Phone.Marketplace.LicenseInformation();
            if(license.IsTrial())
            {
                BuyButton.Visibility = Visibility.Visible;
            }

            this.DataContext = InTheHand.ApplicationModel.Package.Current;
            this.DisplayNameText.Text = InTheHand.ApplicationModel.Package.Current.DisplayName;
            this.VersionText.Text = InTheHand.ApplicationModel.Package.Current.Id.Version.ToString(true);
            this.MoreByButton.Content = "More by " + InTheHand.ApplicationModel.Package.Current.PublisherDisplayName;
        }

        private void BuyButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Tasks.MarketplaceDetailTask detailTask = new Microsoft.Phone.Tasks.MarketplaceDetailTask();
            detailTask.Show();
        }

        private void ShareButton_Click(object sender, RoutedEventArgs e)
        {
            DataTransferManager.ShowShareUI();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DataTransferManager.GetForCurrentView().DataRequested += AboutPage_DataRequested;
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            DataTransferManager.GetForCurrentView().DataRequested -= AboutPage_DataRequested;
            base.OnNavigatingFrom(e);
        }

        void AboutPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            args.Request.Data.SetWebLink(new Uri("http://windowsphone.com/s?appid=" + InTheHand.ApplicationModel.Package.Current.Id.ProductId));
            args.Request.Data.Properties.Title = "Check out \"" + InTheHand.ApplicationModel.Package.Current.DisplayName + "\" for Windows Phone";
        }

        private async void MoreByButton_Click(object sender, RoutedEventArgs e)
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("zune:search?publisher=" + InTheHand.ApplicationModel.Package.Current.PublisherDisplayName));
        }
    }
}